home *** CD-ROM | disk | FTP | other *** search
- /* miscellaneous utility functions for helping the higher-level routines
- * browse around in indexed files....
- * 870805-13-... ^z
- */
-
- #include <stdio.h> /* for FILE, getc(), etc. */
- #include <strings.h> /* for strncpy(), etc. */
- #include <ctype.h> /* for isprint(), etc. */
- #include <unix.h> /* for exit(), time(), etc. */
- #include <proto.h> /* for function prototypes */
- #include "brwsr.h" /* for various definitions */
- #include "brwsr.proto.h" /* for my function prototypes */
-
-
-
- /* function to get the 'n'th KEY_REC from key_file and store it in the
- * KEY_REC space pointed to by recp ... note that if an illegal value
- * of 'n' is requested, a 'null' KEY_REC is produced....
- */
-
- void get_key_rec (recp, n)
- KEY_REC *recp;
- register long n;
- {
- extern FILE *key_file;
- extern long min_item[], max_item[];
- char *strncpy();
-
- if (n < min_item[INDEX] || n > max_item[INDEX])
- {
- strncpy ((char *)recp->kkey, " ",
- KEY_LENGTH);
- recp->ccount = 0;
- return;
- }
-
- if (fseek (key_file, sizeof (KEY_REC) * n, 0) != 0)
- {
- beep ();
- printf ("Fatal error in fseek() getting key record #%ld!\n", n);
- exit();
- }
-
- if (fread ((char *)recp, sizeof (KEY_REC), 1, key_file) == 0)
- {
- beep ();
- printf ("Fatal error in fread() getting key record #%ld!\n", n);
- exit();
- }
- }
-
-
- /* routine to get the nth pointer record (a long integer) from ptr_file;
- * the pointer record gives the actual offset in the document file of
- * the nth word in the expanded index....
- */
-
- PTR_REC get_ptr_rec (n)
- register long n;
- {
- PTR_REC p;
- extern FILE *ptr_file;
-
- if (fseek (ptr_file, sizeof (PTR_REC) * n, 0) != 0)
- {
- beep ();
- printf ("Fatal error in fseek() getting pointer record #%ld!\n", n);
- exit();
- }
-
- if (fread ((char *)&p, sizeof (PTR_REC), 1, ptr_file) == 0)
- {
- beep ();
- printf ("Fatal error in fread() getting pointer record #%ld!\n", n);
- exit();
- }
-
- return (p);
- }
-
-
- /* Move backwards in document file to find the start of the line of text
- * which has offset p (from start of file) somewhere in that line.
- * However, don't go back beyond STRLEN characters at a time... that
- * is, put a ceiling of STRLEN on the maximum 'length of a line', to
- * avoid pathological input files with no \n's....
- * Do the work by grabbing a buffer full and then scanning back in that,
- * rather than by laboriously lseek() and fgetc() back one character at
- * a time....
- */
-
- long start_of_line (p)
- register long p;
- {
- extern FILE *doc_file;
- char buf[STRLEN];
- register long i;
- register int n;
-
- if (p <= 0)
- return (0L);
-
- i = p - STRLEN;
- i = ((i < 0) ? 0 : i);
- if (fseek (doc_file, i, 0) != 0)
- {
- beep ();
- printf ("Fatal error in fseek() backing up in document!\n");
- exit();
- }
- if ((n = fread (buf, sizeof (char), p - i, doc_file)) == 0)
- {
- beep ();
- printf ("Fatal error in fread() backing up in document!\n");
- exit();
- }
-
- while (--n >= 0 && buf[n] != '\n');
-
- return (i + n + 1);
- }
-
-
- /* scan forward until reaching the beginning of the next line of text;
- * if at or beyond the end of the file, return a pointer to the last
- * character in the file ...
- * Don't go beyond STRLEN characters, however, in scanning forward....
- */
-
- long next_line (p)
- long p;
- {
- register int c, n;
- extern long max_item[];
-
- if (p >= max_item[TEXT])
- return (max_item[TEXT]);
-
- if (fseek (doc_file, p, 0) != 0)
- {
- beep ();
- printf ("Fatal error in fseek() scanning forward in document!\n");
- exit();
- }
-
- for (n = 0; n < STRLEN; ++n)
- if ((c = fgetc (doc_file)) == EOF || c == '\n')
- break;
-
- return (ftell (doc_file));
- }
-
-
- /* paging James Bond ....
- */
-
- void beep ()
- {
- putchar ('\007');
- }
-
-